package com.hq.door.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hq.door.dto.Pagination;
import com.hq.door.dto.ParentInfoBack;
import com.hq.door.dto.ParentParams;
import com.hq.door.service.IParentService;
import com.hq.door.util.ExcelUtil;

@Controller
@RequestMapping("/parent")
@CrossOrigin(origins={"*"},allowedHeaders={"Access-Control"})
public class ParentBackController {
	/*
	 http://localhost:8080/huaq/find?pageNo=1&pageSize=10&instituteName=&majorName=&gradeName=&className=&info=
	 http://localhost:8080/huaq/delete?id=21
	 http://localhost:8080/huaq/add?parentName=宝宝&mobile=18201102464&address=河北衡水&relation=父子&cardNo=1234
	 http://localhost:8080/huaq/modify?id=20&parentName=袁健宝&mobile=18201102464&address=河北衡水&relation=父子&cardNo=1234
	 http://localhost:8080/huaq/login?name=周会&password=zhouhui
	 */

	@Autowired
	IParentService parentService;
	
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	@ResponseBody
	public String fileUpload(HttpServletRequest request){
		String result = "";
		/*response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=utf-8");*/
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		String path = request.getSession().getServletContext().getRealPath("");
		try {
			List items = upload.parseRequest(request);
			Iterator it = items.iterator();
			while (it.hasNext()) {
				FileItem item = (FileItem) it.next();
				if (item.isFormField()) {
					System.out.println("表单的参数名称：" + item.getFieldName()
							+ ",对应的参数值：" + item.getString("UTF-8"));
				} else {
					if (item.getName() != null && !item.getName().equals("")) {
						System.out.println("上传文件的大小：" + item.getSize());
						System.out.println("上传文件的类型：" + item.getContentType());
						System.out.println("上传文件的名称：" + item.getName());
						  
						File tempFile = new File(item.getName());  
						String savePath = path+File.separator+"backend"+File.separator+"doc"+File.separator
								+new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date())
								+tempFile.getName();
								
						item.write(new File(savePath)); 
						List<Map<String, String>> list = ExcelUtil.readExcel(savePath);
						result = parentService.addParentList(list);
					} else {
						result = "没有选择上传文件!";
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "上传文件不成功！";
		}
		System.out.println(result);
		return "\""+result+"\"";
	}
	
	
	/**
	 * 分页带参查询家长信息
	 * @param pib
	 * @return
	 */
	@RequestMapping(value = "/find", method = RequestMethod.POST)
	@ResponseBody
	public Pagination findParents(ParentParams pib){
		Pagination pagination = parentService.queryParentListByPage(pib);
		if(pagination==null){
			pagination = new Pagination(0,"error",null);
		}
		return pagination;
	}
	
	/**
	 * 查看家长信息
	 * @param pib
	 */
	@RequestMapping(value = "/getParent", method = RequestMethod.POST)
	@ResponseBody
	public ParentInfoBack getParent(Integer id){
		ParentInfoBack pib = parentService.queryParent(id);
		return pib;
	}
	
	/**
	 * 添加一条家长信息
	 * @param pib
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST,produces="text/json;charset=gb2312")
	@ResponseBody
	public String addParent(ParentInfoBack pib){
		String flag = parentService.addParent(pib);
		return "\""+flag+"\"";
	}
	
	/**
	 * 修改一条家长信息
	 * @param pib
	 */
	@RequestMapping(value = "/modify", method = RequestMethod.POST,produces="text/json;charset=gb2312")
	@ResponseBody
	public String modifyParent(ParentInfoBack pib){
		String flag = parentService.modifyParent(pib);
		return "\""+flag+"\"";
	}
	
	/**
	 * 删除家长信息
	 * @param pib
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST,produces="text/json;charset=gb2312")
	@ResponseBody
	public String deleteParent(@RequestParam Integer ...pids){
		Integer flag = parentService.deleteParent(pids);
		return flag.toString();
	}
	
}
